import store from '@/store';


//0.正常,1.没登录,2.超时
export function isLogin() {
    //获取对应的登录信息
    const loginUser = store.state.loginUser
    const loginTime = store.state.loginTime
    console.log(`output->`, new Date().getTime())
    console.log(loginTime)
    
    //如果登录用户不存在则直接返回没登录
    if (!loginUser) return 1

    //如果超时则清空登录信息,并返回超时
    if (new Date().getTime() - loginTime > 30*60* 1000) {
        store.dispatch("logout")
        return 2
    }

    //否则重新更新时间并返回已登录
    store.dispatch("refreshTime")
    return 0
}

export function getToken(){
    const loginUser = store.state.loginUser
    const loginTime = store.state.loginTime
    if(!loginUser) return null
    if(new Date().getTime()-loginTime > 10*1000){
    store.dispatch("logout")
      return null
    }
    store.dispatch("refreshTime")
    return loginUser.token
  }